import { httpClient } from "../httpClient";

// 月度统计概览接口返回类型
export interface MonthlyOverview {
    incomeTotal: number;
    expenseTotal: number;
    monthlyBalance: number;
}

// 类别排行接口返回类型
export interface CategoryRanking {
    name: string;
    amount: number;
    percentage: number;
}

// 支出明细接口返回类型
export interface ExpenseDetail {
    category: string;
    remark: string;
    amount: number;
    date: string;
}

// 类别信息接口返回类型
export interface Category {
    name: string;
    icon: string;
    percentage: number;
}

// 类别详情接口返回类型
export interface CategoryDetails {
    expenses: ExpenseDetail[];
    total: number;
}

// 获取月度账单统计概览
export async function getMonthlyStatistics(month?: string): Promise<MonthlyOverview> {
    const endpoint = month
        ? `/statistics/monthly?month=${month}`
        : '/statistics/monthly';

    const response = await httpClient<{ code: number; data: MonthlyOverview }>(endpoint, "GET");
    return response.data;
}

// 获取支出类别排行
export async function getCategoryRanking(month?: string, limit?: number): Promise<CategoryRanking[]> {
    let endpoint = '/statistics/category-ranking';
    const params = [];

    if (month) params.push(`month=${month}`);
    if (limit) params.push(`limit=${limit}`);

    if (params.length > 0) {
        endpoint += `?${params.join('&')}`;
    }

    const response = await httpClient<{ code: number; data: CategoryRanking[] }>(endpoint, "GET");
    return response.data;
}

// 获取支出明细排行
export async function getExpenseRanking(month?: string, limit?: number): Promise<ExpenseDetail[]> {
    let endpoint = '/statistics/expense-ranking';
    const params = [];

    if (month) params.push(`month=${month}`);
    if (limit) params.push(`limit=${limit}`);

    if (params.length > 0) {
        endpoint += `?${params.join('&')}`;
    }

    const response = await httpClient<{ code: number; data: ExpenseDetail[] }>(endpoint, "GET");
    return response.data;
}

// 获取所有支出类别
export async function getAllCategories(month?: string): Promise<Category[]> {
    const endpoint = month
        ? `/statistics/categories?month=${month}`
        : '/statistics/categories';

    const response = await httpClient<{ code: number; data: Category[] }>(endpoint, "GET");
    return response.data;
}

// 获取指定类别的支出明细
export async function getCategoryDetails(category: string, month?: string): Promise<CategoryDetails> {
    let endpoint = `/statistics/category-details?category=${encodeURIComponent(category)}`;

    if (month) {
        endpoint += `&month=${month}`;
    }

    const response = await httpClient<{ code: number; data: CategoryDetails }>(endpoint, "GET");
    return response.data;
}